package bookstore_mall.exception;

import bookstore_mall.resp.ResponseResult;
import bookstore_mall.resp.ResultCodeEnum;
import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.util.SaResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.nio.file.AccessDeniedException;
import javax.validation.ConstraintViolationException;
/**
 * 全局异常处理程序 统一异常处理器
 *
 * @author 张三丰
 * @date 2023/11/15
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 权限校验异常
     */
    @ExceptionHandler(AccessDeniedException.class)
    public ResponseResult handleAccessDeniedException(AccessDeniedException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',权限校验失败'{}'", requestURI, e.getMessage());
        return ResponseResult.fail(ResultCodeEnum.NOT_AUTH, ResultCodeEnum.getMessageByCode(ResultCodeEnum.NOT_FOUND.getCode()));
    }


    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public ResponseResult handleException(Exception e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}'" +
                ",发生系统异常.", requestURI, e);
        return ResponseResult.fail(ResultCodeEnum.INTERNAL_SERVER_ERROR, ResultCodeEnum.getMessageByCode(ResultCodeEnum.INTERNAL_SERVER_ERROR.getCode()));
    }

    /**
     * 方法参数异常
     *
     * @Valid 注解来标记需要验证的请求参数，如果请求参数验证失败，就会抛出 MethodArgumentNotValidException 异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        String message = e.getBindingResult().getFieldError().getDefaultMessage();
        return ResponseResult.fail(ResultCodeEnum.PARAM_ERROR, message);
    }


    /**
     * 请求参数异常
     *
     * @Valid 注解来标记需要验证的请求参数，如果请求参数验证失败，就会抛出 MethodArgumentNotValidException 异常
     */
    // 处理javax.validation.ConstraintViolationException异常，即参数校验异常
    @ExceptionHandler(ConstraintViolationException.class)
    public ResponseResult<Object> handleConstraintViolationException(ConstraintViolationException e) {
        // 提取错误信息中的关键部分，这里假设错误信息格式如 "getChannelList.page: 页码最大为100"
        // 可以根据实际情况调整提取逻辑
        String[] errorParts = e.getMessage().split(": ");
        String errorMessage = errorParts[1];
        return ResponseResult.fail(ResultCodeEnum.PARAM_ERROR, errorMessage);
    }

    /**
     * 自定义异常类
     */
    @ExceptionHandler(BusinessException.class)
    public ResponseResult BusinessExceptionHandler(BusinessException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发生系统异常.", requestURI, e);
        return ResponseResult.fail(e.getCode(), e.getMessage(), e.getData());
    }

    @ExceptionHandler(SaTokenException.class)
    public SaResult handlerSaTokenException(SaTokenException e) {
        // 默认的提示
        return SaResult.error(e.getMessage());
    }


}
